package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.config.Constants;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.utils.wechat.WeiXinUtils;
import cn.com.connext.scrm.web.rest.vm.FavorStocksVM;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: WxFavorStocksResource
 * @Description: 微信代金券
 * @Author xuebw
 * @Date 2021/9/26
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/wxFavorStocks/")
public class WxFavorStocksResource {

    private final Logger logger = LoggerFactory.getLogger(WxFavorStocksResource.class);

    @Autowired
    private WeiXinUtils weiXinUtils;

    /**
     * 条件查询批次列表API
     * @return
     */
    @PostMapping("queryFavorStocksList")
    @ResponseBody
    public JsonResult queryFavorStocksList(@RequestBody FavorStocksVM vm){
        JsonResult jsonResult = new JsonResult();
        if (vm.getLimit() == null || vm.getOffset() == null) {
            jsonResult.setState(-1);
            jsonResult.setMessage("查批次列表参数不正确");
            return jsonResult;
        }
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode objectNode = objectMapper.createObjectNode();
        StringBuffer stringBuffer = new StringBuffer();
        String url = stringBuffer.append("https://api.mch.weixin.qq.com/v3/marketing/favor/stocks")
            .append("?offset=")
            .append(vm.getOffset())
            .append("&limit=")
            .append(vm.getLimit())
            .append("&stock_creator_mchid=")
            .append(Constants.MERCHANTID).toString();

        String wxHttpClient = weiXinUtils.getWxHttpClient(url, objectMapper, objectNode);
        logger.info("查询批次列表res:" + wxHttpClient);
        if (StringUtils.isNotBlank(wxHttpClient)) {
            JSONObject jsonObject = JSONObject.parseObject(wxHttpClient);
            if (StringUtils.isBlank(jsonObject.getString("total_count"))) {
                jsonResult.setState(JsonResult.ERROR);
                jsonResult.setMessage("查询批次列表失败");
            } else {
                jsonResult.setState(JsonResult.SUCCESS);
                jsonResult.setData(jsonObject);
            }

        } else {
            jsonResult.setState(JsonResult.ERROR);
            jsonResult.setMessage("查询批次列表失败");
        }
        return jsonResult;
    }

    /**
     * 查询批次详情API
     * @return
     */
    @PostMapping("queryFavorStocksDetail")
    @ResponseBody
    public JsonResult queryFavorStocksDetail(@RequestBody FavorStocksVM vm){
        JsonResult jsonResult = new JsonResult();
        if (StringUtils.isEmpty(vm.getStockId())) {
            jsonResult.setState(-1);
            jsonResult.setMessage("查批次详情参数不正确");
            return jsonResult;
        }
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode objectNode = objectMapper.createObjectNode();
        StringBuffer stringBuffer = new StringBuffer();
        String url = stringBuffer.append("https://api.mch.weixin.qq.com/v3/marketing/favor/stocks/")
            .append(vm.getStockId())
            .append("?stock_creator_mchid=")
            .append(Constants.MERCHANTID).toString();

        String wxHttpClient = weiXinUtils.getWxHttpClient(url, objectMapper, objectNode);
        logger.info("查询批次详情res:" + wxHttpClient);
        if (StringUtils.isNotBlank(wxHttpClient)) {
            JSONObject jsonObject = JSONObject.parseObject(wxHttpClient);
            if (StringUtils.isBlank(jsonObject.getString("stock_id"))) {
                jsonResult.setState(JsonResult.ERROR);
                jsonResult.setMessage("查询批次详情失败");
            } else {
                jsonResult.setState(JsonResult.SUCCESS);
                jsonResult.setData(jsonObject);
            }

        } else {
            jsonResult.setState(JsonResult.ERROR);
            jsonResult.setMessage("查询批次详情失败");
        }
        return jsonResult;
    }

    /**
     * 查询代金券详情API
     * @return
     */
    @PostMapping("queryFavorUserCoupons")
    @ResponseBody
    public JsonResult queryFavorUserCoupons(@RequestBody FavorStocksVM vm){
        JsonResult jsonResult = new JsonResult();
        if (StringUtils.isEmpty(vm.getCouponId()) || StringUtils.isEmpty(vm.getOpenId())) {
            jsonResult.setState(-1);
            jsonResult.setMessage("查代金券详情参数不正确");
            return jsonResult;
        }
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode objectNode = objectMapper.createObjectNode();
        StringBuffer stringBuffer = new StringBuffer();
        String url = stringBuffer.append("https://api.mch.weixin.qq.com/v3/marketing/favor/users/")
            .append(vm.getOpenId())
            .append("/coupons/")
            .append(vm.getCouponId())
            .append("?appid=")
            .append(Constants.APPID).toString();

        String wxHttpClient = weiXinUtils.getWxHttpClient(url, objectMapper, objectNode);
        logger.info("查询代金券详情res:" + wxHttpClient);
        if (StringUtils.isNotBlank(wxHttpClient)) {
            JSONObject jsonObject = JSONObject.parseObject(wxHttpClient);
            if (StringUtils.isBlank(jsonObject.getString("stock_id"))) {
                jsonResult.setState(JsonResult.ERROR);
                jsonResult.setMessage("查询代金券详情失败");
            } else {
                jsonResult.setState(JsonResult.SUCCESS);
                jsonResult.setData(jsonObject);
            }

        } else {
            jsonResult.setState(JsonResult.ERROR);
            jsonResult.setMessage("查询代金券详情失败");
        }
        return jsonResult;
    }
}
